package cool.tianyou.blue.bird.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import cool.tianyou.blue.bird.dialog.LoadingDialog;
import cool.tianyou.blue.bird.model.dto.LoginDTO;
import cool.tianyou.blue.bird.codec.digest.DigestUtils;
import cool.tianyou.blue.bird.model.ApiResult;
import cool.tianyou.blue.bird.constant.Constant;
import cool.tianyou.blue.bird.R;
import cool.tianyou.blue.bird.model.vo.LoginVO;
import cool.tianyou.blue.bird.util.HttpUtil;
import cool.tianyou.blue.bird.util.JSONUtil;
import cool.tianyou.blue.bird.util.SpUtil;
import cool.tianyou.blue.bird.util.Util;

import com.xuexiang.xui.XUI;

import java.util.function.Consumer;

public class LoginActivity extends AppCompatActivity {

    private EditText etEmail;
    private EditText etPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XUI.initTheme(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Util.getDeviceId(this);
        init();
        if (Util.checkLoginState(LoginActivity.this)) {
            openMainView();
        }
    }

    private void init() {
        etEmail = findViewById(R.id.et_userName);
        etPassword = findViewById(R.id.et_password);
        Constant.USER_INFO.TOKEN = SpUtil.getString(this, Constant.SP_CONSTANT.TOKEN, "");
        Constant.USER_INFO.USER_ID = SpUtil.getString(this, Constant.SP_CONSTANT.USER_ID, "");
    }

    /**
     * 打开主界面
     */
    public void openMainView() {
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }

    /**
     * 发起登录
     *
     * @param v view
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void login(View v) {
        String email = etEmail.getText().toString();
        String password = etPassword.getText().toString();
        LoadingDialog loadingDialog = new LoadingDialog(this, "正在登录...").setAutoDismiss(5000).showDialog();


        LoginVO mobile = new LoginVO(email,
                DigestUtils.md5Hex(password + "sync_tool" + password),
                Constant.USER_INFO.BRAND,
                "mobile",
                Util.getDeviceId(this));

        HttpUtil.post(this, mobile, "/account/login", (Consumer<ApiResult<?>>) apiResult -> {
            Looper.prepare();
            loadingDialog.dismiss();
            if (apiResult.getCode() == 200) {
                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
                LoginDTO loginDTO = JSONUtil.parseObject(apiResult.getData(), LoginDTO.class);
                Constant.saveUserInfo(loginDTO, LoginActivity.this);
                openMainView();
            } else {
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
            }
            Looper.loop();
        });

    }
}
